package com.dyj.ads
import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.{DataFrame, SparkSession}

object ads_e_mz_au5800_xt {
  def main(args: Array[String]): Unit = {
    val ds: String = args(0)

    val sparkSession: SparkSession = SparkSession.builder()
      .appName("血糖指标统计")
      .enableHiveSupport()
      .config("spark.sql.shuffle.partitions", 1)
      .getOrCreate()

    import sparkSession.implicits._
    import org.apache.spark.sql.functions._

    sparkSession.sql("use bigdata03_dws")

    val xtDF: DataFrame = sparkSession.sql(
      s"""
        |select
        |baoGaoBianHao,
        |fenZhongXueTang120,
        |baoGaoRiQi,
        |xt_max,
        |xt_min,
        |xt_avg
        |from
        |bigdata03_dws.dws_e_mz_au5800shenghua
        |where
        |ds='${ds}'
        |""".stripMargin)

    xtDF
      .withColumn("number", count($"baoGaoBianHao") over Window.partitionBy($"baoGaoBianHao"))
      .withColumn("e_fc", ($"fenZhongXueTang120" - $"xt_avg") * ($"fenZhongXueTang120" - $"xt_avg"))
      .withColumn("a_fc", sum($"e_fc").over())
      .withColumn("xt_fc", $"a_fc" / $"number")
      .withColumn("xt_bzc", sqrt($"xt_fc"))
      .withColumn("xt_ycgs", sum(when($"fenZhongXueTang120" >= 11.1, 1)).over())
      .select($"baoGaoBianHao", $"fenZhongXueTang120", $"baoGaoRiQi", $"xt_max", $"xt_min", $"xt_avg", $"xt_fc", $"xt_bzc", $"xt_ycgs")
      .write
      .format("csv")
      .save("/daas/motl/bigdata03/ads/ads_e_mz_au5800_xt/ds=" + ds)
  }

}
